

import { createRouter, createWebHashHistory } from 'vue-router'
import { routesCfg as constantRoutes } from './routes'
import Strorge from '@/utils/storage'

// 初始化路由
export const router = createRouter({
  history: createWebHashHistory( '/' ),
  routes: constantRoutes,
  scrollBehavior( to, from, savedPosition ) {
    if ( savedPosition ) {
      return savedPosition
    }
    return { top: 0, left: 0 }

  }
})

router.beforeEach( async ( to, from ) => {
  // const token = Strorge.get( 'token' )

  // if ( '/login' !== to.path && !token ) {
  //   return { path: '/login', query: { redirect: to.fullPath } }
  // }

  // // 防止登录后再次返回/login
  // if ( '/login' == to.path && token ) {
  //   return { path: '/user' }
  // }

  return true
})

// 当前应用配置路由
export function setupRouter( app_ ) {
  app_.use( router )
}
